<template>
  <div>
    <van-nav-bar title="云朵头条-登录" />
    <van-form @submit="onSubmit" ref="loginFormRef">
      <van-field v-model="user.mobile" name="mobile" label="手机号" placeholder="请输入11位手机号"
        :rules="[{ required: true, message: '手机号不合法', pattern: /^1[3-9]\d{9}$/ }]" required />
      <van-field v-model="user.code" type="code" name="code" label="密码" placeholder="请输入6位密码"
        :rules="[{ required: true, message: '密码不合法', pattern: /^\d{6}$/ }]" required />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit " :disabled="isloading" :loading="isloading"
          loading-text="加载中...">登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { setToken } from '../utils/token.js'
export default {
  name: 'homeView',
  data() {
    return {
      user: {
        mobile: '',
        code: ''// 验证码
      },
      isloading: false

    }
  },
  methods: {
    async onSubmit() {
      this.isloading = true
      try {
        const { data: res } = await this.$http.post('/v1_0/authorizations', this.user)
        console.log(res)
        this.$msg({ type: 'success', message: '登陆成功' })
        // window.sessionStorage.setItem('token', res.data.token)
        setToken(res.data.token)
        localStorage.setItem('refreshToken', res.data.refresh_token)
        this.$router.push('/layout/home')
      } catch (err) {
        this.$msg({ type: 'danger', message: '账号或密码错误' })
      }
      this.isloading = false
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #1989fa;
  font-size: 20px;

}

/deep/.van-nav-bar_title {
  // 要修改组件内样式，如果用了scoped,就需要在选择器前加/deep/即可
  color: #fff;
}
</style>
